#include <cstdio>
#include <cstring>
#include <queue>
#define N 1010
using namespace std;

int n, m;
int cnt = -1, heads[N];
struct Edge {
	int u, v, nxt;
} e[100010];
inline void add(int u, int v) {
	e[++cnt].v = v;
	e[cnt].u = u;
	e[cnt].nxt = heads[u];
	heads[u] = cnt;
}
bool v[N];
int dis[N];
int main() {
	// freopen("testdata.in", "r", stdin);
	// freopen("testdata.out", "w", stdout);
	freopen("railway.in", "r", stdin);
	freopen("railway.out", "w", stdout);
	scanf("%d%d", &n, &m);
	int tmp1, tmp2;
	memset(heads, -1, sizeof(heads));
	for (int i = 0; i < m; ++i) {
		scanf("%d %d", &tmp1, &tmp2);
		add(tmp1, tmp2);
	}
	for (int i = 0; i <= cnt; ++i) {
		queue< int > q;
		memset(v, 0, sizeof(v));
		memset(dis, -1, sizeof(dis));
		q.push(e[i].u);
		dis[e[i].u] = 0;
		while (!q.empty()) {
			int y = q.front();
			q.pop();
			for (int j = heads[y]; ~j; j = e[j].nxt) {
				if (j == i) continue;
				if (!v[e[j].v]) {
					q.push(e[j].v);
					dis[e[j].v] = dis[y] + 1;
					v[e[j].v] = true;
				}
				if (e[j].v == e[i].v) goto fin;
			}
		}
		fin:
		printf("%d ", dis[e[i].v]);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}